<script setup>
	import {onLaunch,onHide,onShow,onLoad} from "@dcloudio/uni-app";
	import {useUserStore} from "@/stores/user.js";
	
	const userStore = useUserStore()


	const unIdCo = uniCloud.importObject('uni-id-co')

	onLaunch(() => {
		console.log('====== App Launch')
		
		const tokenExpire = uni.getStorageSync("uni_id_token_expired")
		if(!login) {
			onsole.log('token不存在，，重定向登录页面')
			login()
		}
		const diff = tokenExpire - new Date()
		if(diff < 0) {
			console.log('token过期，重定向登录页面')
			login()
		}
	

	})

	onShow(() => {
		console.log('====== App Show')
	})

	onHide(() => {
		console.log('====== App Hide')
	})
	
	
	const login =()=>{
		uni.login({
			success: (res) => {
				// {errMsg: "login:ok", code: "0c3yF7000jy62T15xj2002YXCe2yF70N"}
				unIdCo.loginByWeixin({
					// 参考文档：https://doc.dcloud.net.cn/uniCloud/uni-id/summary.html
					code: res.code,
				}).then(result => {
					console.log('登录成功', result)
					if (result.errCode === 0) {
						// 如果不想展示登录成功, 展开属性，同时增加个属性showToast
						// userStore.loginSuccess({...result,showToast:false})
						userStore.loginSuccess(result)
					}
				}).catch(err => {
					console.log('登录失败', err)
				})
			}
		})
	}
	
	
</script>

<style lang="scss">
	@import url("common/style/common-style.scss");
	@import "@/static/fonts/iconfont.css"; // css中会引入iconfont.ttf
</style>